/**
 * 
 */
package com.htht.tourism.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.htht.tourism.domain.Response;
import com.htht.tourism.domain.Sources;
import com.htht.tourism.domain.User;
import com.htht.tourism.domain.link.Activity_signParam;
import com.htht.tourism.domain.link.LoginUser;
import com.htht.tourism.domain.link.SourcesParam;
import com.htht.tourism.domain.link.UserParam;
import com.htht.tourism.intf.TUserService;
import com.htht.tourism.utils.ConfigUtil;
import com.htht.tourism.utils.ValidatorResultHandler;

/**用户管理
 * @author wuqiwei
 *
 */
@Controller
@ResponseBody
@RequestMapping("/user")
public class TuserController {
	@Resource
	private TUserService tUserService;
	/**
	 * 添加用户信息
	 * @author:wuqiwei
	 * @param @param param
	 * @param @param result
	 * @param @return
	 * @date:2017年3月17日下午3:27:04
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@RequestBody @Validated UserParam param,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			int a = this.tUserService.add(param);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("添加失败！");
			}
		} else {
			return retVal;
		}
	}
	
	/**
	 * 修改用户信息
	 * @author:wuqiwei
	 * @param @param param
	 * @param @param result
	 * @param @return
	 * @date:2017年3月17日下午3:31:32
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response update(@RequestBody @Validated UserParam param,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			String coin = param.getCoin();
			String str = coin.substring(21);
			param.setCoin(str);
			int a = this.tUserService.update(param);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("修改失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 查询用户信息
	 * @author:wuqiwei
	 * @param @param param
	 * @param @param result
	 * @param @return
	 * @date:2017年3月17日下午3:59:50
	 */
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public Response get(@RequestBody @Validated UserParam param,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			Map map = this.tUserService.get(param);
			if (map.size() > 0) {
				return Response.getInstance().success(map);
			} else {
				return Response.getInstance().failure("查询失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 删除用户信息
	 * @author:wuqiwei
	 * @param @param param
	 * @param @param result
	 * @param @return
	 * @date:2017年3月17日下午4:30:09
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Response del(@RequestBody @Validated UserParam param,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			int a = this.tUserService.del(param);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("删除失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 上传头像
	 * @author:wuqiwei
	 * @param @param file
	 * @param @param request
	 * @param @return
	 * @date:2017年3月24日下午2:56:21
	 */
	@RequestMapping(value = "/ImgUpload", method = RequestMethod.POST)
	public Response enterpriseUploadHeading(
			@RequestParam(value = "file",required = false) MultipartFile file,HttpServletRequest request){
		String path = request.getSession().getServletContext().getRealPath(ConfigUtil.getProperty("default","sources"));
		Date date = new Date();
		String time =  String.valueOf(date.getTime());
		String filename = file.getOriginalFilename();//文件的名字
		
		UserParam param = new UserParam();
		String id = request.getParameter("id");
		param.setId(Long.valueOf(id));
		param.setCoin("MountainTourism"+ConfigUtil.getProperty("default","sources")+time+filename);
		File target = new File(path,time+filename);
		if(!target.exists()){
			target.mkdirs();
		}
		try {
			file.transferTo(target);
			List<User> list = tUserService.getALL(param);//查询是否含有这条记录
			if(list.size() > 0){//判断当大于0时 修改这条记录
				int b = tUserService.update(param);//修改信息
			}else{
				return Response.getInstance().success("添加失败");
			}
			return Response.getInstance().success("MountainTourism"+ConfigUtil.getProperty("default","sources")+time+filename);
		} catch (IllegalStateException e) {
			return Response.getInstance().failure(e.getMessage());
		} catch (IOException e) {
			return Response.getInstance().failure(e.getMessage());
		}
	}
	/**
	 * 用户登录
	 * @author:wuqiwei
	 * @param @param param
	 * @param @param result
	 * @param @return
	 * @date:2017年3月30日上午9:38:31
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Response login(@RequestBody @Validated UserParam param,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			List<LoginUser> list = this.tUserService.login(param);
			if (list.size() > 0) {
				return Response.getInstance().success(list);
			} else {
				return Response.getInstance().failure("登录失败！");
			}
		} else {
			return retVal;
		}
	}
}
